//
//  WYCRepaymentController.m
//  信用卡管理
//
//  Created by 中企互联 on 2017/11/14.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCIntelligentMoneyController.h"
#import "MyTextField.h"
#import "WYCSeeBillModel.h"
#import "LDCalendarView.h"
#import "NSDate+extend.h"
#import "WYCRepayModel.h"
#import "WYCTRepayCell.h"
#import "WYCSelectionPlanController.h"
#import "WYCPreviewController.h"
#import "repayModel.h"
#import "WYCPlanModel.h"
#import "WYCPlandetaiLCell.h"
#import "amountModel.h"
#import "WYCArgeeViewController.h"
#import "OYRPopOption.h"
#import "WYCHandBillController.h"
#import "WYCBillController.h"
#import "UIViewController+HUD.h"
@interface WYCIntelligentMoneyController ()<UITableViewDelegate,UITableViewDataSource,WYCSelectionPlanDelegate>
{
    
   int  summer;
    
    int maxNum;
    UILabel * backLabel;
    
    
    double string;
}
@property(nonatomic,strong) UILabel * typelabel;

@property (nonatomic, strong)LDCalendarView *calendarView;//日历控件
@property (nonatomic, strong)NSMutableArray *seletedDays;//选择的日期
@property (nonatomic, strong)NSMutableArray *dataArray;//数据数组

@property (nonatomic, copy)NSString *showStr;

/** planID*/
@property(nonatomic,strong)NSString * planID;

/** 时间*/
@property(nonatomic,strong)UILabel * timeLbel;

/** 模型赋值*/
@property(nonatomic,strong)WYCSeeBillModel * seeBillModel;

/** 模型二*/
@property (strong , nonatomic)WYCRepayModel * repayModel2;
@property(nonatomic,strong)UITableView * tableView;

/** cell1 金额输入框*/
@property(nonatomic,strong)UITextField *repaymentTextField;


/** cell2 余额输入框*/
@property(nonatomic,strong)UITextField *balanceTextField;


/** MyTextField*/
@property(nonatomic,strong)MyTextField *repayTextField;


@property (nonatomic,strong) NSMutableArray *repayarray;

@property (nonatomic,strong) NSMutableArray *datearray;
@property (nonatomic,strong) NSMutableArray *poundagearray;

@property (nonatomic,strong) NSMutableArray *detArr;




/** 日期*/
@property(nonatomic,strong)UILabel *daylabel;

/** UILabel * backLabel*/
@property(nonatomic,strong)UILabel * backLabel;

@end

@implementation WYCIntelligentMoneyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCbgColor;
    _poundagearray = [[NSMutableArray alloc]init];
    self.datearray = [NSMutableArray new];
    self.dataArray  = [NSMutableArray new];
    
    //Nav
    [self setupNav];
    
    //请求
    [self setupRequster];
    
    self.repayarray = [NSMutableArray new];
    _datearray = [[NSMutableArray alloc]init];
    
    WYCRepayModel * repayModel = [[WYCRepayModel alloc]init];
    repayModel.title = @"还款金额:";
    repayModel.isenland = NO;
    repayModel.titleFeild = @"请输入还款金额";
    [self.repayarray addObject:repayModel];
    
    
    self.repayModel2 = [[WYCRepayModel alloc]init];
    self.repayModel2.title = @"卡内余额:";
    self.repayModel2.isenland = NO;
    self.repayModel2.titleFeild = @"请输入卡内余额";
    [self.repayarray addObject:self.repayModel2];
    [self.repayModel2 addObserver:self forKeyPath:@"coment" options:NSKeyValueObservingOptionNew context:nil];
    
    WYCRepayModel * repayModel3 = [[WYCRepayModel alloc]init];
    repayModel3.title = @"还款天数：";
    repayModel3.isenland = YES;
    [self.repayarray addObject:repayModel3];
    
    
    WYCRepayModel * repayModel4 = [[WYCRepayModel alloc]init];
    repayModel4.title = @"消费日期:";
    repayModel4.titleFeild = @"选择还款日期";
    repayModel4.isenland = YES;
    [self.repayarray addObject:repayModel4];
    
    
    WYCRepayModel * repayModel5 = [[WYCRepayModel alloc]init];
    repayModel5.title = @"消费计划:";
    repayModel5.titleFeild = @"选择消费计划";
    repayModel5.isenland = YES;
    [self.repayarray addObject:repayModel5];
    
    
    
    
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"TextField" object:nil];
    
    WYCSelectionPlanController * selection = [[WYCSelectionPlanController alloc] init];
    selection.Delegate = self;
    
    //头部界面创建
    //    [self setupCreat];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    WYCLog(@"-----------");
    WYCRepayModel *model1 = self.repayarray[0];
    
    WYCRepayModel *model2 = self.repayarray[1];
    
    WYCRepayModel * model3 = self.repayarray[2];
    
    WYCRepayModel * model4 = self.repayarray[3];
    
    
    int balanceintValue = [model2.coment intValue];
    
    int huankuan = [model1.coment intValue];
    
    int onestr = huankuan * 0.06;
    
    int str = balanceintValue;
    
    
    if (str < onestr) {
        
        [self showToast:@"余额不能低于还款金额的6%"];
        return;
    }
}


- (void)test{
    
   
    WYCRepayModel *model1 = self.repayarray[0];
    WYCRepayModel *model2 = self.repayarray[1];
    WYCRepayModel *model3 = self.repayarray[2];
    
    int repaymentintValue = [model1.coment intValue];
    
    int balanceintValue = [model2.coment intValue];
    
    if (repaymentintValue == 0 || balanceintValue == 0) {
        model3.coment = @"";
    }else{
        
        
        WYCRepayModel *astr = self.repayarray[0];
        double str = [astr.coment doubleValue];
        double str2 = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
        
        string = (str * str2) / 100;
        
        backLabel.text = [NSString stringWithFormat:@"本期账单消费手续费：%.2f元",string];
        double  dascount = repaymentintValue / balanceintValue - string;
        
        NSString * stringDday = [NSString stringWithFormat:@"%.2f",dascount];
        
        NSArray * dayarray = [stringDday componentsSeparatedByString:@"."];
        if (dayarray[1] != 0) {
            double sm = [dayarray[0] intValue] + 1;
            double i =  (balanceintValue - string - sm) * 1;
            double n = repaymentintValue / i;
            NSString * nStr = [NSString stringWithFormat:@"%.2f",n];
            NSArray * narr = [nStr componentsSeparatedByString:@"."];
            
            if (narr[1] != 0) {
                summer = [narr[0] intValue] + 1;
                maxNum = summer;
                model3.coment = [NSString stringWithFormat:@"%d",summer];
                
                WYCLog(@"不是0model3是%@",model3.coment);
            }
            
        }else{
            summer = [dayarray[0] intValue];
            model3.coment = [NSString stringWithFormat:@"%d",summer];
            WYCLog(@"是0model3是%@",model3.coment);
            
        }
        
        //        model3.coment = [NSString stringWithFormat:@"%d",summer];
        
    }
    
    
    WYCRepayModel *astr = self.repayarray[0];
    double str = [astr.coment doubleValue];
    double str2 = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
    
    string = str * str2 / 100;
    backLabel.text = [NSString stringWithFormat:@"本期账单消费手续费：%.2f元",string];
    
    
    
}

- (NSString *)showStr {
    NSMutableString *str = [NSMutableString string];
    
    [str appendString:@""];
    //从小到大排序
    [self.seletedDays sortUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
        return [obj1 compare:obj2];
    }];
    for (NSNumber *interval in self.seletedDays) {
        NSString *partStr = [NSDate stringWithTimestamp:interval.doubleValue/1000.0 format:@"yyyy-MM-dd"];
        [str appendFormat:@"%@,",partStr];
        [self.tableView reloadData];
    }
    return [str copy];
}

#pragma make -------->>请求数据
- (void)setupRequster{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = self.cardId;
    params[@"token"] = [WYCAccountTool unarchivetoken];
    
    [HttpTool POST:creditcarddetails params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = responseObject[@"result"];
            self.seeBillModel = [WYCSeeBillModel mj_objectWithKeyValues:dict];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        }
        
        //界面
        [self setupCreat];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

#pragma make ---------->>头部界面创建
- (void)setupCreat{
    UIImageView * backView = [[UIImageView alloc] init];
    backView.frame = CGRectMake(0, NavHeight, ScreenWidth, 200);
    backView.image =[UIImage imageNamed:@"backbg"];
    backView.backgroundColor = WYCMain;
    [self.view addSubview:backView];
    
    
    
    UIView * backView2 = [[UIView alloc] init];
    backView2.frame = CGRectMake(20, 20, ScreenWidth-40, 160);
    backView2.backgroundColor = [UIColor whiteColor];
    backView2.layer.cornerRadius = 30 * px;
    [backView addSubview:backView2];
    
    UIView * backView1 = [[UIView alloc] init];
    backView1.frame = CGRectMake(0, 0, ScreenWidth-40, 40);
    backView1.backgroundColor = wycMain;
    [backView2 addSubview:backView1];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:backView1.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5,5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = backView1.bounds;
    maskLayer.path = maskPath.CGPath;
    backView1.layer.mask = maskLayer;
    
    UILabel *titleStr = [[UILabel alloc] init];
    titleStr.frame = CGRectMake(10, 10, backView1.width-20, 20);
    titleStr.text =  [NSString stringWithFormat:@"%@(%@)",self.bankName,self.lastNumber];
    titleStr.textAlignment = NSTextAlignmentLeft;
    titleStr.textColor = [UIColor whiteColor];
    titleStr.font = [UIFont systemFontOfSize:15];
    [backView1 addSubview:titleStr];
    
    UILabel *benqizhangdan = [[UILabel alloc] init];
    benqizhangdan.frame = CGRectMake(10, CGRectGetMaxY(backView1.frame) + 10, backView1.width-10, 20);
    benqizhangdan.text = @"本期应还";
    benqizhangdan.textAlignment = NSTextAlignmentLeft;
    benqizhangdan.textColor = [UIColor grayColor];
    benqizhangdan.font = [UIFont systemFontOfSize:13];
    [backView2 addSubview:benqizhangdan];
    
    UILabel * balancelabel = [[UILabel alloc] init];
    balancelabel.frame = CGRectMake(10,benqizhangdan.frame.size.height+benqizhangdan.frame.origin.y+10 , 600  * px, 70 * px);
    NSString * billAmount = [NSString stringWithFormat:@"%.1f",[self.billAmount doubleValue]];
    balancelabel.text = billAmount;
    balancelabel.textColor = WYCMain;
    balancelabel.textAlignment = NSTextAlignmentLeft;
    balancelabel.font = [UIFont systemFontOfSize:25];
    [backView2 addSubview:balancelabel];
    
    //还款状态
    self.typelabel = [[UILabel alloc] init];
    self.typelabel.frame = CGRectMake(ScreenWidth-160, CGRectGetMaxY(backView1.frame) + 10, 100, 20);
    self.typelabel.textColor = [UIColor whiteColor];
    self.typelabel.layer.masksToBounds = YES;
    self.typelabel.textAlignment = NSTextAlignmentCenter;
    self.typelabel.layer.cornerRadius = 10;
    self.typelabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:self.typelabel];
    
    if ([self.bankState isEqualToString:@"0"]){
        
        self.typelabel.text = @"";
        self.typelabel.backgroundColor = [UIColor clearColor];
    } else if ([self.bankState isEqualToString:@"1"]){
        NSString * aooroveStr = [NSString stringWithFormat:@"待还款"];
        self.typelabel.text = aooroveStr;
        self.typelabel.backgroundColor = WYCMain;
    }else if ([self.bankState isEqualToString:@"2"]){
        NSString * aooroveStr = [NSString stringWithFormat:@"已还清"];
        self.typelabel.text = aooroveStr;
        self.typelabel.backgroundColor = [UIColor greenColor];
    }else if ([self.bankState isEqualToString:@"3"]){
        NSString * aooroveStr = [NSString stringWithFormat:@"智能还款中"];
        self.typelabel.text = aooroveStr;
        self.typelabel.backgroundColor = wycBlue;
    }else{
        NSString * aooroveStr = [NSString stringWithFormat:@"智能还款中止"];
        self.typelabel.text = aooroveStr;
        self.typelabel.backgroundColor = WYCMain;
        
    }
    
    //账单日
    UILabel *Statementlabel = [[UILabel alloc] init];
    Statementlabel.frame = CGRectMake(0, backView2.height-45, backView2.width / 4, 50 * px);
    Statementlabel.text = @"账单日";
    Statementlabel.textAlignment = NSTextAlignmentCenter;
    Statementlabel.textColor = [UIColor grayColor];
    Statementlabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Statementlabel];
    
    
    UILabel *Statement = [[UILabel alloc] init];
    Statement.frame = CGRectMake(Statementlabel.x, CGRectGetMaxY(Statementlabel.frame) + 20 * px, Statementlabel.width, Statementlabel.height);
    Statement.text = [NSString stringWithFormat:@"%@",self.billDate];
    Statement.textAlignment = NSTextAlignmentCenter;
    Statement.textColor = [UIColor blackColor];
    Statement.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Statement];
    
    //还款日
    UILabel *Repaymentlabel = [[UILabel alloc] init];
    Repaymentlabel.frame = CGRectMake(backView2.width / 4, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Repaymentlabel.text = @"还款日";
    Repaymentlabel.textAlignment = NSTextAlignmentCenter;
    Repaymentlabel.textColor = [UIColor grayColor];
    Repaymentlabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Repaymentlabel];
    
    
    UILabel *Repayment = [[UILabel alloc] init];
    Repayment.frame = CGRectMake(Repaymentlabel.x, CGRectGetMaxY(Repaymentlabel.frame) + 20 * px, Statementlabel.width, Statementlabel.height);
    NSString * dueDate = [NSString stringWithFormat:@"%@",self.dueDate];
    Repayment.text = dueDate;
    Repayment.textColor = [UIColor blackColor];
    Repayment.textAlignment = NSTextAlignmentCenter;
    Repayment.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Repayment];
    
    
    //免息期
    UILabel *Gracelabel = [[UILabel alloc] init];
    Gracelabel.frame = CGRectMake(backView2.width / 2, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Gracelabel.text = @"免息期";
    Gracelabel.textAlignment = NSTextAlignmentCenter;
    Gracelabel.textColor = [UIColor grayColor];
    Gracelabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Gracelabel];
    
    
    UILabel *Grace = [[UILabel alloc] init];
    Grace.frame = CGRectMake(Gracelabel.x, CGRectGetMaxY(Gracelabel.frame) + 20 * px, Statementlabel.width, Statementlabel.height);
    NSString * gracePeriod = [NSString stringWithFormat:@"%@",self.gracePeriod];
    Grace.text = gracePeriod;
    Grace.textColor = [UIColor blackColor];
    Grace.textAlignment = NSTextAlignmentCenter;
    Grace.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Grace];
    
    
    
    //总额度
    UILabel *Totallabel = [[UILabel alloc] init];
    Totallabel.frame = CGRectMake(backView2.width / 4 * 3, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Totallabel.text = @"总额度";
    Totallabel.textAlignment = NSTextAlignmentCenter;
    Totallabel.textColor = [UIColor grayColor];
    Totallabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Totallabel];
    
    
    UILabel *Total = [[UILabel alloc] init];
    Total.frame = CGRectMake(Totallabel.x, CGRectGetMaxY(Totallabel.frame) + 20 * px, Statementlabel.width, Statementlabel.height);
    Total.textAlignment = NSTextAlignmentCenter;
    NSString * creditLine = [NSString stringWithFormat:@"%.1f",[self.creditLine doubleValue]];
    Total.text = creditLine;
    Total.textColor = [UIColor blackColor];
    Total.font = [UIFont systemFontOfSize:SmallFont];
    [backView2 addSubview:Total];
    
    UIView  *lineV =[[UIView alloc]init];
    lineV.backgroundColor = WYZGrayBg;
    lineV.frame = CGRectMake(0, Statementlabel.y-5, backView2.frame.size.width, 1);
    [backView2 addSubview:lineV];

    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backView.frame)+10, ScreenWidth,  180 * 5 * px) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.tableFooterView.height = 120 * px;
    [self.view addSubview:self.tableView];
    
    
    backLabel = [[UILabel alloc] init];
    backLabel.textAlignment = NSTextAlignmentLeft;
       backLabel.backgroundColor = [UIColor clearColor];
    backLabel.text = @"总计手续费：--";
    backLabel.font = [UIFont systemFontOfSize:SmallFont];
    backLabel.textColor = [UIColor clearColor];
    backLabel.frame = CGRectMake(40 * px, CGRectGetMaxY(self.tableView.frame) + 80 * px, ScreenWidth - 80 * px, 48 * px);
    [self.view addSubview:backLabel];
    
    UIButton * returnButton = [[UIButton alloc] init];
    returnButton.frame = CGRectMake( 40 * px,  CGRectGetMaxY(self.tableView.frame) + 80 * px, ScreenWidth - 80 * px, 150 * px);
    returnButton.backgroundColor = wycMain;
    [returnButton setTitle:@"生成智能还款计划" forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [returnButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:returnButton];
    
    //用户须知的按钮
    UIButton * userAgreeBtn = [[UIButton alloc] init];
    userAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:43 * px];
    userAgreeBtn.frame = CGRectMake(returnButton.x + 80,CGRectGetMaxY(returnButton.frame) + 20 * px,20, 20);
    [userAgreeBtn setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
    [self.view addSubview:userAgreeBtn];
    
    UILabel * PromptLable = [[UILabel alloc] init];
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:SmallFont];
    NSString * str = @"我已阅读并同意";
    PromptLable.font = fnt;
    PromptLable.x = userAgreeBtn.x + 30;
    PromptLable.y = CGRectGetMaxY(returnButton.frame) + 30 * px;
    CGSize PromptLableSize = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    PromptLable.text = str;
    PromptLable.textColor = WYCRGBColor(157, 157, 157);
    PromptLable.frame = CGRectMake(PromptLable.x, PromptLable.y, PromptLableSize.width, PromptLableSize.height);
    [self.view addSubview:PromptLable];
    
    
    
    //用户须知的按钮
    UIButton * userAgreement = [[UIButton alloc] init];
    [userAgreement setTitle:@"《用户协议》" forState:UIControlStateNormal];
    userAgreement.titleLabel.font = [UIFont systemFontOfSize:43 * px];
    userAgreement.frame = CGRectMake(CGRectGetMaxX(PromptLable.frame), PromptLable.y, PromptLable.width + 90 * px, PromptLable.height);
    userAgreement.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userAgreement setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [userAgreement addTarget:self action:@selector(pressAgree) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:userAgreement];
}
#pragma make ----------->>Nav
- (void)setupNav{
 
    //右侧按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"Three" hight:@"Three" target:self action:@selector(ThreeClick:)];
    
}
- (void)ThreeClick:(UIButton *)optionButton{
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    CGRect frame = [optionButton convertRect:optionButton.bounds toView:window];
    
    
    OYRPopOption *s = [[OYRPopOption alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    
    s.option_optionContents = @[@"隐藏该卡", @"手输账单"];
    // 使用链式语法直接展示 无需再写 [s option_show];
    [[s option_setupPopOption:^(NSInteger index, NSString *content) {
        
        
        if (index == 0) {
            
            NSMutableDictionary * params = [NSMutableDictionary dictionary];
            params[@"token"] = [WYCAccountTool unarchivetoken];
            params[@"cardId"] = self.cardId;
            params[@"isHideCard"] = @"1";
            [HttpTool POST:creditcardsetHideCard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
                
                if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                    
                    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"成功"];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
                    
                    for (UIViewController *controller in self.navigationController.viewControllers) {
                        if ([controller isKindOfClass:[WYCBillController class]]) {
                            [self.navigationController popToViewController:controller animated:YES];
                            break;
                        }
                    }
                    WYCBillController *SendDrugVC = [[WYCBillController alloc]init];
                    [self.navigationController popToViewController:SendDrugVC animated:YES];
                    
                }else{
                    
                    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
                    
                }
                
            } fail:^(NSURLSessionDataTask *task, NSError *error) {
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
                
            }];
            
        }else{
            
            WYCHandBillController * OperationBll = [[WYCHandBillController alloc] init];
            [self.navigationController pushViewController:OperationBll animated:NO];
            
        }
        
    } whichFrame:frame animate:YES] option_show];
    
}

//用户协议
-(void)pressAgree
{
    WYCArgeeViewController  *agree = [[WYCArgeeViewController alloc]init];
    [self.navigationController pushViewController:agree animated:YES];
}
#pragma ----------->数据元代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 180 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    tableView.tableFooterView = [[UIView alloc] init];
    
    WYCTRepayCell * cell = [WYCTRepayCell cellWithTableView:tableView];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.repaymodel = self.repayarray[indexPath.row];
    if (indexPath.row == 4 || indexPath.row == 3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    }else{
        
    }
    
    
    [cell.addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    //    [cell.riliButton addTarget:self action:@selector(CalculatorButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [cell.rustButton addTarget:self action:@selector(reduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

#pragma make --------->>日历选择
- (void)CalculatorButtonClick{
    
    WYCLogFunc;
    WYCRepayModel *model1 = self.repayarray[0];
    WYCRepayModel *model2 = self.repayarray[1];

    int huankuan = [model1.coment intValue];
    //卡内余额
    int balanceintValue = [model2.coment intValue];

    int onestr = huankuan *0.06 ;
    int str = balanceintValue;

    if (str < onestr ) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"余额不能低于还款金额的6%"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        return;
    }else{
    if (!_calendarView) {
        _calendarView = [[LDCalendarView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
        [self.view addSubview:_calendarView];
        
        __weak typeof(self) weakSelf = self;
        _calendarView.complete = ^(NSArray *result) {
            if (result.count != 0) {
                weakSelf.seletedDays = [result mutableCopy];
                WYCRepayModel * model3 = weakSelf.repayarray[3];
                NSString * str = [NSString stringWithFormat:@"%@",weakSelf.showStr];
                NSString *b = [str substringToIndex:[str length]-1];
                model3.coment = b;
                [weakSelf.tableView reloadData];
            }
        };
    }
    [self.calendarView show];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYCRepayModel *model1 = self.repayarray[0];
    
    WYCRepayModel *model2 = self.repayarray[1];
    
    WYCRepayModel * model3 = self.repayarray[2];
    
    WYCRepayModel * model4 = self.repayarray[3];
    if (indexPath.row == 3) {
        [self CalculatorButtonClick];
    }
    
    if (indexPath.row == 4) {
        WYCLogFunc;
        if (model1.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款金额不能为空"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            return;
        }
        if (model2.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"卡内余额不能为空"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            return;
        }
        if (model3.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款天数不能为空"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            return;
        }
        
        if (model4.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"消费日期不能为空"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            return;
        }
        
        WYCSelectionPlanController * SetUsercontroller = [[WYCSelectionPlanController alloc] init];
        SetUsercontroller.bankId = self.seeBillModel.bankId;
        SetUsercontroller.Delegate = self;
        
        int repaymentintValue = [model1.coment intValue];
        
        int repaymentintValue2 = [model3.coment intValue];
        
        int str = repaymentintValue / repaymentintValue2;
        
        SetUsercontroller.dailyAmount = [NSString stringWithFormat:@"%d",str];
        
        SetUsercontroller.balance = [NSString stringWithFormat:@"%d",repaymentintValue];
        
        [self.navigationController pushViewController:SetUsercontroller animated:NO];
        
        
        // 取消选中状态
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
    
}
#pragma make --------->>加号按钮
- (void)addButtonClick{
    WYCLogFunc;

    WYCRepayModel * model3 =self.repayarray[2];
    summer = [model3.coment intValue];
    if ([model3.coment intValue]<maxNum) {
        summer++;

    }
    model3.coment = [NSString stringWithFormat:@"%d",summer];
    
    [self.tableView reloadData];
    
}

#pragma make --------->>减号按钮
- (void)reduceButtonClick{
    WYCLogFunc;
    WYCRepayModel * model3 =self.repayarray[2];
    summer = [model3.coment intValue];
    if (summer == 1) {
        return;
    }
    summer--;
    model3.coment = [NSString stringWithFormat:@"%d",summer];
    
    [self.tableView reloadData];
    
}

-(void)nextplan{
    WYCPreviewController  *preView = [[WYCPreviewController alloc]init];
    [self.navigationController pushViewController:preView animated:YES];
    
}
//生成智能还款计划
- (void)returnButtonClick{
    WYCRepayModel *model0 = self.repayarray[0];
    
    WYCRepayModel *model1 = self.repayarray[1];
    
    WYCRepayModel * model3 = self.repayarray[3];
    
    if (model0.coment.length == 0) {
        
        NSString * showStr = @"还款金额不能为空";
      [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        return;
    }
   else if (model1.coment.length == 0) {
        
        NSString * showStr = @"卡内金额不能为空";
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
       [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
       [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
       [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        return;
    }
   else if (model3.coment.length == 0) {
       
       NSString * showStr = @"消费日期不能为空";
       [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
       [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
       [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
       [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
       return;
   }
   else if (self.planID.length == 0) {
       
       NSString * showStr = @"消费计划不能为空";
       [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
       [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
       [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
       [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
       return;
   }
    else{
    
    NSString * str = [NSString stringWithFormat:@"%@",self.showStr];
    NSString *b = [str substringToIndex:[str length]-1];
    model3.coment = b;
    WYCRepayModel * model2 = self.repayarray[2];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardId"] = self.seeBillModel.cardId;
    params[@"amount"] = model0.coment;
    params[@"balance"] = model1.coment;
    params[@"days"] = model2.coment;
    params[@"consumeDates"] = model3.coment;
    params[@"consumePlanId"] = self.planID;
        WYCLog(@"params%@",params);
        if ([self.bankState isEqualToString:@"1"] || [self.bankState isEqualToString:@"3"]) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"已存在的待执行的计划"];
            return;
        }else{
        WYCPreviewController  *preView = [[WYCPreviewController alloc]init];
        preView.parmDic = params;
        [self.navigationController pushViewController:preView animated:YES];
        }

    }
}

- (void)setDelegatemaxNumber:(NSString *)maxNumber setID:(NSString *)ID{
    WYCRepayModel * model4= self.repayarray[4];
    model4.coment = [NSString stringWithFormat:@"%@",maxNumber];
    
    self.planID = ID;
    [self.tableView reloadData];
}
-(void)setupbank:(NSString *)strbank
{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"planNo"] = strbank;
    [HttpTool POST:epaymentplanbank params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        WYCLog(@"-----------responseobject%@",responseObject);
        NSLog(@"计划创建返回信息%@",[responseObject objectForKey:@"msg"]);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"计划创建成功"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            [self.navigationController popViewControllerAnimated:YES];

        }
    
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"计划创建失败"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色


    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}



- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCMain;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCMain.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:backbutton];
    
    
//        //返回按钮
//        UIButton *  fanhuibutton = [[UIButton alloc] init];
//        [fanhuibutton setImage:[UIImage imageNamed:@"Three"] forState:UIControlStateNormal];
//        [fanhuibutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
//        fanhuibutton.frame = CGRectMake(NavView.width - 96 * px, 30, 70 * px , 70 * px);
//        [NavView addSubview:fanhuibutton];
    //
    //    UIImageView *title = [[UIImageView alloc]init];
    //    title.frame =CGRectMake(ScreenWidth/2-35, 20, 70, 23);
    //    title.image = [UIImage imageNamed:@"name"];
    //    [NavView addSubview:title];
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"账单管理";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)dealloc {
    
    [self.repayModel2 removeObserver:self forKeyPath:@"coment"];
}
@end

